﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Controls;
using Model.Modules;
using Utility;

namespace SteelDetection.UIControl
{
    /// <summary>
    /// UCRasterImgViewer.xaml 的交互逻辑
    /// </summary>
    public partial class UCRasterImgViewer : UserControl
    {
        ImageViewer Viewer;
        bool ViewerLoaded=false;
        
        public UCRasterImgViewer()
        {
            InitializeComponent();
            //this.SetBinding(ImageProperty, "Image");
            //viewergrid.DataContext = this;
            Loaded += (s, e) => {
                initViewer();
            };
            Unloaded+= (s, e) => {
                ViewerLoaded = false;
                Viewer.Dispose();
                Viewer = null;
            };
        }
        void initViewer() {
            Viewer = new ImageViewer()
            {
                Background = new SolidColorBrush(Colors.Transparent),
                ViewPadding = new ControlPadding(),
                ViewHorizontalAlignment = ControlAlignment.Center,
                ViewVerticalAlignment = ControlAlignment.Center,
            };
            Viewer.Loaded += (s, e) => {
                ViewerLoaded = true;
                if(ImageInfo!=null)
                    SetImage(ImageInfo);
            };
            Viewer.Zoom(ControlSizeMode.FitAlways, 1, Viewer.DefaultZoomOrigin);
            ImageViewerPanZoomInteractiveMode imageViewerPanZoomInteractiveMode = new ImageViewerPanZoomInteractiveMode();
            
            // Add the Interactive Mode to the Image Viewer 
            Viewer.InteractiveModes.BeginUpdate();
            Viewer.InteractiveModes.Add(imageViewerPanZoomInteractiveMode);
            Viewer.InteractiveModes.EndUpdate();
            Viewer.ScrollMode = ControlScrollMode.Hidden;
            viewergrid.Children.Clear();
            viewergrid.Children.Add(Viewer);

        }

        public RasterImage Image
        {
            get
            {
                return (RasterImage)GetValue(ImageProperty);
            }
            set
            {
                SetValue(ImageProperty, value);
            }
        }
        public static readonly DependencyProperty ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(RasterImage), typeof(UCRasterImgViewer), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((s, e) => {
            var ucRasterImgViewer = s as UCRasterImgViewer;
            if(ucRasterImgViewer.ViewerLoaded==true)
                ucRasterImgViewer.Viewer.Image = e.NewValue as RasterImage;
        })));

        public Imagedefectinfo ImageInfo
        {
            get
            {
                return (Imagedefectinfo)GetValue(ImageInfoProperty);
            }
            set
            {
                SetValue(ImageInfoProperty, value);
            }
        }
        public static readonly DependencyProperty ImageInfoProperty = DependencyProperty.RegisterAttached("ImageInfo", typeof(Imagedefectinfo), typeof(UCRasterImgViewer), new FrameworkPropertyMetadata(null, new PropertyChangedCallback((s, e) => {
            var ucRasterImgViewer = s as UCRasterImgViewer;
            if (ucRasterImgViewer.ViewerLoaded == true)
            {
                if (e.NewValue !=null&& File.Exists((e.NewValue as Imagedefectinfo).CurrentImage.image_url.ToString()))
                {
                  
                    Logger.Info((e.NewValue as Imagedefectinfo).CurrentImage.image_url.ToString());
                    ucRasterImgViewer.SetImage(e.NewValue as Imagedefectinfo);
                }
            }

                
        })));


        private void SetImage(Imagedefectinfo imageinfo)
        {
            if(imageinfo==null|| imageinfo.CurrentImage == null||!File.Exists(imageinfo.CurrentImage.image_url) )
                Viewer.Image = null;
            else
            {
                try
                {
                    Viewer.Image = new RasterCodecs().Load(imageinfo.CurrentImage.image_url.ToString());
                }
                catch {
                    Viewer.Image = null;
                }

            }
        }

    }
}
